namespace NBody.Analysis
{
    using System;
    using DataFile;
    
    public abstract class EnergyBin : Bin
    {
        public enum Orbit { Radial, Isotropic };
                
        protected Orbit orbit = Orbit.Radial;
        
        public Orbit BinOrbit
        {
            get { return orbit; }
            set { orbit = value; }
        }
        
        protected void energy_bin_normal(NBodySystem s, Function f)
        {
            Console.Error.WriteLine("  Binning in energy with normal spacing");
            
            // calculate total energies.
            double e_tot;
            Energy.CalculateTotalEnergy(ref s, out e_tot);
            
            // find max, min e
            double emin = ((EnergyParticle)s[0]).Energy;
            double emax = emin;
            foreach (EnergyParticle p in s)
            {
                if (p.Energy > emax)
                    emax = p.Energy;
                if (p.Energy < emin)
                    emin = p.Energy;
            }
            emax += 1e-10;
            Console.Error.WriteLine("  Emin = {0:f6}\n  Emax = {1:f6}", emin, emax);
            
            double delta_e = (emax - emin) / (double)n;
            data = new Table(n, 2, "Energy, Binned quantity");
            data.Clear();
            for (int i = 0; i < n; i++)
                data[i, 0] = emin + 0.5 * delta_e + i * delta_e;

            foreach (EnergyParticle p in s)
            {
                int pos = (int)((p.Energy - emin) / delta_e);
                data[pos, 1] += f(p);
            }
        }
    }
}   		
